package com.dsjy.yf.address.component;

import com.dsjy.yf.address.entity.dataobject.CommunityDO;
import com.dsjy.yf.address.entity.dataobject.CountyDO;
import com.dsjy.yf.address.entity.dataobject.TownDO;
import com.dsjy.yf.address.service.CommunityService;
import com.dsjy.yf.address.service.CountyService;
import com.dsjy.yf.address.service.TownService;
import com.dsjy.yf.address.service.impl.AddressSplitServiceImpl;
import com.dsjy.yf.address.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-14 16:02
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Autowired
    private CountyService countyService;
    @Autowired
    private TownService townService;
    @Autowired
    private CommunityService communityService;

    /**
     * 初始化资源,预加载各区县名及code
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        FileUtil.creatFileDir(FileUtil.TEM_PATH);
        initCounty();
        initTown();
        initCommunityListMap();
    }

    /**
     * 初始化countyMap
     */
    public void initCounty() {
        List<CountyDO> countyDOList = countyService.searchAll();
        AddressSplitServiceImpl.countyMap = new HashMap<>();
        AddressSplitServiceImpl.countyCodeMap = new HashMap<>();
        AddressSplitServiceImpl.countyList = new ArrayList<>();
        for (CountyDO countyDO : countyDOList) {
            AddressSplitServiceImpl.countyMap.put(countyDO.getCountyName(), countyDO.getCountyCode());
            AddressSplitServiceImpl.countyCodeMap.put(countyDO.getCountyCode(), countyDO.getCountyName());
            AddressSplitServiceImpl.countyList.add(countyDO);
        }
    }

    /**
     * 初始化townListMap
     */
    public void initTown() {
        List<TownDO> townDOList = townService.searchAll();
        AddressSplitServiceImpl.townListMap = new HashMap<>();
        AddressSplitServiceImpl.townCodeMap = new HashMap<>();
        AddressSplitServiceImpl.townNameCodeMap = new HashMap<>();
        for (TownDO townDO : townDOList) {
            AddressSplitServiceImpl.townCodeMap.put(townDO.getTownCode(), townDO.getTownName());
            AddressSplitServiceImpl.townNameCodeMap.put(townDO.getTownName(), townDO.getTownCode());
            if (AddressSplitServiceImpl.townListMap.containsKey(townDO.getParentCode())) {
                AddressSplitServiceImpl.townListMap.get(townDO.getParentCode()).add(townDO);
            } else {
                List<TownDO> list = new ArrayList<>();
                list.add(townDO);
                AddressSplitServiceImpl.townListMap.put(townDO.getParentCode(), list);
            }
        }
    }

    /**
     * 初始化communityListMap
     */
    public void initCommunityListMap() {
        List<CommunityDO> communityDOList = communityService.searchAll();
        AddressSplitServiceImpl.communityListMap = new HashMap<>(1000);
        for (CommunityDO communityDO : communityDOList) {
            if (AddressSplitServiceImpl.communityListMap.containsKey(communityDO.getParentCode())) {
                AddressSplitServiceImpl.communityListMap.get(communityDO.getParentCode()).add(communityDO);
            } else {
                List<CommunityDO> list = new ArrayList<>();
                list.add(communityDO);
                AddressSplitServiceImpl.communityListMap.put(communityDO.getParentCode(), list);
            }
        }

    }

}
